import * as types  from '../constants/userTypes';
import {getItem} from '../utils';

const initialObject = {
    userInfo: {
        nickname: '',
        phone: '',
        avatar: ''
    },
    famList: [],
    orderList: [],
    wxConfig: {},
    wxPayConfig: {}
};
const initialState = getItem('user') || initialObject;
export default function USER(state = initialState, action) {
    switch (action.type) {

        case types.LOGIN:
            return {
                ...state,
                // userInfo: action.data.data
            };

        case types.BIND_PHONE:
            return {
                ...state,
                userInfo: {
                    ...state.userInfo,
                    ...action.data.data
                }
            };

        case types.FETCH_WX_SIGN:
            return {
                ...state,
                wxConfig: action.data.data
            };
        case types.FETCH_WX_PAY_SIGN:
            return {
                ...state,
                wxPayConfig: action.data.data
            };


        case types.FETCH_USER_INFO:
            return {
                ...state,
                userInfo: action.userInfo
            };
        case types.CHANGE_USER_INFO:
            return {
                ...state,
                // userInfo: action.userInfo
            };
        case types.CHANGE_PHONE_NUMBER:
            return {
                ...state,
                // userInfo: action.userInfo
            };
        case types.SEND_SMS_CODE:
            return {
                ...state,
                // userInfo: action.userInfo
            };


        case types.FETCH_FAM_LIST:
            return {
                ...state,
                famList: action.data.data.array
            };
        case types.ADD_FAM_MEMBER:
            return {
                ...state,
                famList: [
                    ...state.famList,
                    action.member
                ]
            };
        case types.EDIT_FAM_MEMBER:
            return {
                ...state,
                famList: state.famList.map(item=> item.id === action.member.id ? {
                    ...item,
                    name: action.member.name,
                    phone: action.member.phone,
                    relation: action.member.relation,
                    birthday: action.member.birthday,
                    sex: action.member.sex
                } : item)

            };
        case types.DEL_FAM_MEMBER:
            return {
                ...state,
                famList: state.famList.filter(item=>item.id !== action.memberId)
            };


        case types.FETCH_ORDER_LIST:
            return {
                ...state,
                orderList: action.orderList
            };
        case types.FETCH_ORDER_DETAIL:
            return {
                ...state,
                // orderList: action.orderList
            };
        case types.PAY_ORDER:
            return {
                ...state,
                data: action.response
            };
        case types.CANCEL_ORDER:
            return {
                ...state,
                orderList: state.orderList.map(item=> item.number === action.data.data.number ? {
                    ...item,
                    payStatus: 103
                } : item)

            };


        case types.LOGOUT:

        default:
            return state;
    }
}